<template>
  <div>
    <FormPanel
      ref="baseInfo"
      :list="initFormBase"
      :cols="3"
      :labelWidth="60"
      :isSubmitBtn="false"
      @formChange="formChangeHandleBase"
    />
    <BreakSpace label="经销商搜索" :containerStyle="{ marginBottom: '10px' }" />
    <!-- 经销商名称\代码搜索 -->
    <TopFilter
      :list="topFilterInit"
      :cols="2"
      :collapse="collapse"
      labelWidth="90"
      @filterChange="changeHandle"
    ></TopFilter>
    <!-- table -->
    <FilterTable
      height="500"
      ref="listModal"
      :columns="columns"
      :selectionType="selectType"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      :onRowSelectChange="selectChange"
  columnsRef="cms-common_dealerdata_index_01"      
    ></FilterTable>
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">确定</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getDealerList ,pageDealerInfoList ,getRegion, getCommunity ,getProvinceByLoginDeptId} from '@/api/cms-common/dealerdata';
import { DICT_CODE } from '@/api/cms-api/origin/commonDealerType';		//经销商类型工具类
import { region } from '@/api/cms-common/region';//行政区域
import { insertConfig} from '@/api/potenCus/basedata/clueDistributeConfig/index';

export default {
  mixins: [dictionary,region],
  props: {
    value: {
      type: Object,
      required: true
    },
    selectType:{
      type: String,
      default: 'single'
    }
  },
  data() {
    return {
      excludeBigCustomer:this.value.excludeBigCustomer ? 25031004 : null,
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi:pageDealerInfoList,
      fetchParams: {
        excludeBigCustomer: this.value.excludeBigCustomer ? 25031004 : null,
        isDeleted: 0,//未删除
        status:10031001,//启用
        auditStatus:25511005,//已审核
        deptId:this.value.userInfo.orgId,
        'queryByOrgCode':'byOrgCode',
        'querybyExcludeSD':'byExcludeSD'
      }, //ipa 接口
      selectedRows: [],//选中经销商
      initFormBase: this.initFormBaseFun(),     //基本信息-初始化控件
    };
  },
  created() {
    //初始化省份信息
    this.initProvince();
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        // { type: 'SELECT', label: '事业部', fieldName: 'deptId', placeholder: '请选择', initialValue: '' ,itemList:[]},
        { type: 'INPUT', label: '经销商代码', fieldName: 'dealerCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '经销商简称', fieldName: 'dealerShortname', placeholder: '请输入', initialValue: '' }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      let extP = {
        excludeBigCustomer: this.value.excludeBigCustomer ? 25031004 : null,
        isDeleted: 0,//未删除
        status:10031001,//启用
        auditStatus:25511005,//已审核
        deptId:this.value.userInfo.orgId,
        'queryByOrgCode':'byOrgCode',
        'querybyExcludeSD':'byExcludeSD'
      }
      this.fetchParams = Object.assign({},val,extP);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
          { title: '事业部', dataIndex: 'deptName', sorter: true ,width:200},
        { title: '经销商代码', dataIndex: 'dealerCode', sorter: true },
        { title: '公司全称', dataIndex: 'dealerName', sorter: true },
        // { title: '经销商简称', dataIndex: 'dealerShortname', sorter: true },
        // { title: '经销商类型', dataIndex: 'dealerType', sorter: true, render: props => {
        //     return <span>{this.createDictText(props.row.dealerType, DICT_CODE)}</span>;
        //   } },
        // { title: '经销商级别', dataIndex: 'shopLevel', sorter: true, render: props => {
        //     return <span>{this.createDictText(props.row.shopLevel, '2540')}</span>;
        //   } },
        // { title: '省份', dataIndex: 'provinceId', sorter: true , render: props => {
        //     return <span>{this.createDictText(props.row.provinceId, '1056')}</span>;
        //   }},
        // { title: '城市', dataIndex: 'cityId', sorter: true ,render: props => {
        //     return <span>{this.createDictText(props.row.cityId, '1056')}</span>;
        //   }},
        // { title: '县区', dataIndex: 'countyId', sorter: true ,render: props => {
        //     return <span>{this.createDictText(props.row.countyId, '1056')}</span>;
        //   }}
          // { title: '省份', dataIndex: 'provinceId', sorter: true,
					// 	render: props => {
					// 		return <span>{this.createRegionText(props.row.provinceId)}</span>;
					// 	}
					// },
					// { title: '城市', dataIndex: 'cityId', sorter: true,
					// 	render: props => {
					// 		return <span>{this.createRegionText(props.row.cityId)}</span>;
					// 	}
					// },
					// { title: '县区', dataIndex: 'districtId', sorter: true,
					// 	render: props => {
					// 		return <span>{this.createRegionText(props.row.districtId)}</span>;
					// 	}
					// }
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    saveDialog() {
      if(this.selectedRows.length == 0){
        this.$notify.warning({ title: '提示信息', message: '请勾选经销商!' });
        return;
      }
      console.log(this.selectedRows,"===");
      this.$refs.baseInfo.SUBMIT_FORM();


      // this.$emit('close', { visible: false ,"data":this.selectedRows});
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false });
    },
    // 基础信息表单数据获取  
    async formChangeHandleBase(data){
      console.log("-=-=-data:",data);
      let config = {};
      config.deptId = this.value.userInfo.orgId;
      config.provinceId = data.provinceId;
      config.cityId = data.cityId;
      config.countyId = data.countyId;
      config.ownerCode = this.selectedRows[0].dealerCode;
      config.dealerName = this.selectedRows[0].dealerName;
      config.areaNum = 1;
      config.noAreaNum = 1;
      config.isBlacklist = 10041002;
      config.isNew = 10041001;
      let res = await insertConfig(config);
      if (res.resultCode === 200) {
        this.$notify.success({title: '提示信息',message:'新增分配经销商成功!'});
        this.$emit('close', { visible: false ,type:'refresh'});
      }else{
        this.$notify.error({title: '提示信息',message:res.errMsg||'新增分配经销商失败!'});
      }

    },
    //初始化所在省份
    async initProvince(){
      let res = await getProvinceByLoginDeptId();
      if (res.resultCode === 200) {
        this.initFormBase.find(item => item.fieldName === 'provinceId').itemList = res.data.map(x => ({ value: x.regionId, text: x.regionName }));
      }
    },
    //初始化所在市数据
    async initCity(provinceId,flag = true){
      if(flag){
        //清空市
        this.initFormBase.find(item => item.fieldName === 'cityId').initialValue="";
        this.initFormBase.find(item => item.fieldName === 'cityId').itemList = [];
        //清空县
        this.initFormBase.find(item => item.fieldName === 'countyId').initialValue="";
        this.initFormBase.find(item => item.fieldName === 'countyId').itemList=[];
      }

      if(!provinceId || provinceId==null || typeof(provinceId)=='undefined'){
        return;
      }
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      this.initFormBase.find(item => item.fieldName === 'cityId').itemList = cl;
    },
    //初始化所在县数据
    async initTown(cityId,flag = true){
      if(flag){
        //清空县
        this.initFormBase.find(item => item.fieldName === 'countyId').initialValue="";
        this.initFormBase.find(item => item.fieldName === 'countyId').itemList=[];
      }
      if(!cityId || cityId==null || typeof(cityId)=='undefined'){
        return;
      }
      let td = this.cityData[cityId].children;
      let tl = this.createSubRegionList(td);
      this.initFormBase.find(item => item.fieldName === 'countyId').itemList = tl;
    },
    //基本信息数据
    initFormBaseFun() {
      return [
         {//10
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          initialValue: "",
          change: (value) => {
            this.initCity(value)
          },
          itemList:  [],
          rules: [{
            required: true,
            message: '必填',
            trigger: 'blur'
          }]
        },
        {//11
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          initialValue: "",
          itemList: [],
          change: (value) => {
            this.initTown(value)
          },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {//12
          type: 'SELECT',
          label: '县区',
          fieldName: 'countyId',
          placeholder: '请选择',
          initialValue: "",
          itemList:  [],
          
        },
        
      ];
    },

  },
  mounted() {
  }
};
</script>

<style lang="less" scoped>
</style>
